package com.yk.airline.component.autoemails.impl;

import com.yk.airline.component.autoemails.api.IMailSender;
import com.yk.airline.util.PropertiesUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Project Name:airlinecrawler
 * File Name:MailSenderImpl
 * Package Name:com.yk.airline.component.autoemails.impl
 * Date:2018/2/7 11:39
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2018  ,  重庆云凯科技有限公司 All Rights Reserved.
 */
@Component("mailSenderImpl")
public class MailSenderImpl implements IMailSender {

    private static Logger log = Logger.getLogger(MailSenderImpl.class);
    @Autowired
    private JavaMailSenderImpl mailSender;

    @Override
    public void sendMail(String title , String context , List<String> fileList) {
        //读取配置文件中的收件人
        System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
        Map properties = PropertiesUtil.getProperties("mail.properties");
        String[] to = ((String)properties.get("mail.smtp.to")).split(",");// 收件人
//        String[] cc = ((String)properties.get("mail.smtp.cc")).split(",");// 收件人
        MimeMessage mailMessage = mailSender.createMimeMessage();
        log.info("发送邮件给" + properties.get("mail.smtp.to"));
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "utf-8");
            helper.setFrom((String) properties.get("mail.smtp.from"));// 设置发件人
            helper.setTo(to);// 设置收件人
//            helper.setCc(cc);// 设置抄送
            helper.setSubject(title);// 设置主题
            helper.setText(context);// 邮件体
            if (fileList != null){
                fileList.stream().forEach(item->{
                    try {
                        File tempFile = new File(item);
                        String fileName = tempFile.getName();
                        helper.addAttachment(MimeUtility.encodeText(fileName) , tempFile);
                        log.info("向邮件中添加了附加 ：" +  fileName);
                    }catch (Exception e){
                        log.error("向邮件中添加附件发生异常->" + e.getMessage());
                        e.printStackTrace();
                    }
                });
            }
            mailSender.send(mailMessage);// 发送邮件
            log.info("邮件发送成功...");
        } catch (Exception e) {
            log.error("邮件发送发生异常");
            log.error(e.getMessage());
            log.info("进行重发");
            try {
                Thread.sleep(1000 * 1000);
                this.sendMail(title,context,fileList);
            } catch (InterruptedException e1) {
                log.info("重发邮件发生异常");
                log.error(e.getMessage());
                e1.printStackTrace();
            }
        }
    }

}
